//
//  ProgressControl.m
//  iCodeMac
//
//  Created by Andrew Barry on 6/04/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "ProgressControl.h"


@implementation ProgressControl
-(id) init {
	self = [super init];
	minValue = 0;
	maxValue = 10;
	value = 5;
	return self;
}

-(NSArray *) getEvents {
	return [NSArray arrayWithObjects:@"Click", nil];
}

-(NSView *) load: (NSDictionary *) defn withFrame: (NSRect) frame {
	if ([defn objectForKey: @"minimum"])
		minValue = [[defn objectForKey: @"minimum"] intValue];
	if ([defn objectForKey: @"maximum"])
		maxValue = [[defn objectForKey: @"maximum"] intValue];
	if ([defn objectForKey: @"value"])
		value = [[defn objectForKey: @"value"] intValue];
	
	NSProgressIndicator *vw = [[NSProgressIndicator alloc] initWithFrame:frame];
	if ([controlType isEqualToString:@"spinner"]) {
		[vw setStyle:NSProgressIndicatorSpinningStyle];
	}
	[vw setMinValue:minValue];
	[vw setMaxValue:maxValue];
	[vw setDoubleValue:value];

	return vw;
}

-(id) valueForProperty: (NSString *) name {
	if ([name isEqualToString:@"minimum"]) {
		return [NSNumber numberWithInt:minValue];
	} else if ([name isEqualToString:@"maximum"]) {
		return [NSNumber numberWithInt:maxValue];
	} else if ([name isEqualToString:@"value"]) {
		return [NSNumber numberWithInt:value];
	}
	return [super valueForProperty: name];
}

-(void) setValue: (id) val forProperty:(NSString*) name {
	if ([name isEqualToString:@"minimum"]) {
		minValue = [val intValue];
	} else if ([name isEqualToString:@"maximum"]) {
		maxValue = [val intValue];
	} else if ([name isEqualToString:@"value"]) {
		value = [val intValue];
	} else {
		[super setValue: val forProperty: name];
	}
}

-(void) saveFields: (NSMutableDictionary *) content {
	[content setObject: [NSNumber numberWithInt:minValue] forKey: @"minimum"];
	[content setObject: [NSNumber numberWithInt:maxValue] forKey: @"maximum"];
	[content setObject: [NSNumber numberWithInt:value] forKey: @"value"];
	[super saveFields: content];
}

-(NSString *) propsNibName {
	if ([controlType isEqualToString:@"spinner"]) {
		return @"ViewProps";
	} else {
		return @"ProgressProps";
	}
}

@end
